package cn.zhf.shop.service.impl;

import cn.zhf.shop.entity.Campus;
import cn.zhf.shop.entity.Message;
import cn.zhf.shop.entity.campus.CampusOverview;
import cn.zhf.shop.entity.lesson.Lesson;
import cn.zhf.shop.entity.lesson.LessonOverview;
import cn.zhf.shop.entity.lesson.LessonTypeOverview;
import cn.zhf.shop.entity.organization.OrgCampus;
import cn.zhf.shop.entity.organization.OrgTypeOverview;
import cn.zhf.shop.entity.organization.OrganizationOverview;
import cn.zhf.shop.mapper.*;
import cn.zhf.shop.service.OrgCampusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;


@Component
public class OrgCampusServiceImpl implements OrgCampusService {
    @Resource
    private OrgCampusMapper orgCampusMapper;
    private Logger logger= LoggerFactory.getLogger(getClass());
    @Resource
    private OrganizationTypeMapper typeMapper;
    @Resource
    private CampusMapper campusMapper;
    @Resource
    private LessonMapper lessonMapper;
    @Resource
    private LessonTypeMapper lessonTypeMapper;



    @Override
    public Message<List<OrganizationOverview>> getRecommend(String area, String provice, String city, String district) {
        List<OrgCampus> refund=orgCampusMapper.selectByLocation(area,provice,city,district);

        List<OrganizationOverview> overviewsList= dataAdapter2(refund);
        return overviewsList==null?Message.fail("无记录"):Message.success(null).add(overviewsList);
    }

    @Override
    public Message<List<OrganizationOverview>> getByLessonType(String type) {
        List<OrgCampus> refund=orgCampusMapper.selectByLessonType("%"+type+"%");
        List<OrganizationOverview> overviewsList= dataAdapter(refund);
        return overviewsList==null?Message.fail("无记录"):Message.success(null).add(overviewsList);
    }

    @Override
    public Message<List<OrganizationOverview>> getByOrgId(Integer id) {
        List<OrgCampus> refund=orgCampusMapper.selectByOrg(id);
        List<OrganizationOverview> overviewsList= dataAdapter(refund);
        return overviewsList==null?Message.fail("无记录"):Message.success(null).add(overviewsList);
    }

    @Override
    public Message<CampusOverview> getByCampusId(Integer id) {
        Campus campus = campusMapper.selectByPrimaryKey(id);
        logger.info("campus:"+campus.getCampusId());
        List<LessonOverview> lessons=new ArrayList<>();
        List<Lesson> lessonList=lessonMapper.selectOverviewByCampus(campus.getCampusName());
        for (Lesson item:lessonList) {
            List<LessonTypeOverview> lessonTypeList=new ArrayList<>();
            String[] lessonTypes=item.getLessonType().split(";");
            for (String type:lessonTypes) {
                String typeName=lessonTypeMapper.selectByPrimaryKey(Integer.parseInt(type)).getTypeName();
                lessonTypeList.add(new LessonTypeOverview(typeName));
            }
            lessons.add(new LessonOverview(
                            item.getLessonId(),
                            item.getLessonName(),
                            lessonTypeList,
                            item.getLessonPrice(),
                            item.getLessonImg(),
                            item.getBuyCount()
                    )
            );
        }
        CampusOverview campusOverview=new CampusOverview(
                campus.getCampusId(),
                campus.getCampusName(),
                campus.getCampusImg(),
                campus.getCampusInfo(),
                campus.getCampusState(),
                campus.getCampusHours(),
                campus.getArea()+campus.getProvice()+campus.getCity()+campus.getDistrict(),
                lessons
        );
        logger.info("campus:"+campusOverview.toString());
        return Message.success(null).add(campusOverview);
    }

    private List<OrganizationOverview> dataAdapter(List<OrgCampus> refund){
        List<OrganizationOverview> overviewsList=new ArrayList<>();
        for(OrgCampus item:refund){
            List<OrgTypeOverview> types=new ArrayList<>();
            String[] TypesS=item.getOrgType().split(";");
            for (String type:TypesS) {

               types.add(new OrgTypeOverview(typeMapper.selectByPrimaryKey(Integer.parseInt(type)).getTypeName()));

            }
            OrganizationOverview overview=new OrganizationOverview(item.getCampusId(), item.getCampusName(), item.getLoaction(), item.getCampusImg(), types);
            overviewsList.add(overview);
        }
        return overviewsList;
    }

    private List<OrganizationOverview> dataAdapter2(List<OrgCampus> refund){
        List<OrganizationOverview> overviewsList=new ArrayList<>();
        for(OrgCampus item:refund){
            List<OrgTypeOverview> types=new ArrayList<>();
            String[] TypesS=item.getOrgType().split(";");
            for (String type:TypesS) {

                types.add(new OrgTypeOverview(lessonTypeMapper.selectByPrimaryKey(Integer.parseInt(type)).getTypeName()));
            }
            OrganizationOverview overview=new OrganizationOverview(item.getCampusId(), item.getCampusName(), item.getLoaction(), item.getCampusImg(), types);
            overviewsList.add(overview);
        }
        return overviewsList;
    }
}

